<?php
require_once 'config.php';

$cible = 'http://api.worldoftanks.eu/2.0/encyclopedia/tanks/?application_id=d0a293dc77667c9328783d489c8cef73&language=fr&fields=tank_id,name,nation,level,type,is_premium,name_i18n';

if($_SERVER['HTTP_HOST'] === 'localhost') {
    $cible = 'json/tanklist.json';
}
$dataTank = file_get_contents($cible);
$listeTank = json_decode($dataTank, true);

$baseUri = 'http://worldoftanks.eu/static/2.9.0/encyclopedia/tankopedia/vehicle/';

$params = $application['bdd'];
$dsn = $params['type'].':host='.$params['serveur'].';dbname='.$params['instance'];
$user = $params['user'];
$pwd = $params['pwd'];
$options = null;
$pdo = new PDO($dsn, $user, $pwd, $options);
$res = $pdo->exec('delete from tanks');

foreach ($listeTank['data'] as $tank) {
    if (substr($tank['name'], 1, 2) === 'gb') {
        $tank['name'] = '#uk'.substr($tank['name'], 3);
    }
    $exceptions = array(
                    11841 => '#france-bat_chatillon155_58',
                    6209 => '#france-f10_amx_50b',
                    9217 => '#ussr-object252',
                    2113 => '#france-_105_lefh18b2'
    );
    if (key_exists($tank['tank_id'], $exceptions) === true) {
        $tank['name'] = $exceptions[$tank['tank_id']];
    }
    $img = strtolower(str_replace(array('_vehicles:_', '_vehicles:'), '-', substr($tank['name'],1))).'.png';
    $premium = 1;
    if ($tank['is_premium'] === false) {
        $premium = 0;
    }
    $bind = array(':tankid' => $tank['tank_id'],
            ':name' => $tank['name_i18n'],
            ':nation' => $tank['nation'],
            ':level' => $tank['level'],
            ':type' => $tank['type'],
            ':image' => $baseUri.$img,
            ':contour' => $baseUri.'contour/'.$img,
            ':small' => $baseUri.'small/'.$img,
            ':premium' => $premium);
    
    echo '<img src="'.$baseUri.$img.'" alt="'.$tank['name_i18n'].'" title="'.$tank['name'].'"/>';
    $sql = 'INSERT INTO tanks
    			VALUES (:tankid, :name, :nation, :level, :type, :image, :contour, :small, :premium)';
    
    $resultat = $pdo->prepare($sql, array(PDO::ATTR_CURSOR => PDO::CURSOR_FWDONLY));
    $execution = $resultat->execute($bind);
    
    
    // $insert = $this->executerSql($sql, $bind);
}
$pdo = null;


//https://api.worldoftanks.eu/wot/encyclopedia/tankinfo/?application_id=demo&language=fr&fields=crew&tank_id=2113